#!/bin/sh
#
# Copyright (c) 2023 Ken McDonell.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# Wrapper script to start or Stop the Performance Co-Pilot pmie
# processes.  The real work is done in $PCP_SERVICES_DIR/pmie.
#
# The following is for chkconfig on RedHat based systems
# chkconfig: 2345 94 06
# description: pmie is a performance inference engine for the Performance Co-Pilot (PCP)
#
# The following is for insserv(1) based systems,
# e.g. SuSE, where chkconfig is a perl script.
### BEGIN INIT INFO
# Provides:       pmie
# Required-Start: $remote_fs
# Should-Start: $local_fs $network $syslog $time $pmcd
# Required-Stop: $remote_fs
# Should-Stop:  $local_fs $network $syslog $pmcd
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Control pmie (performance inference engine for PCP)
# Description:       Configure and control pmie (the performance inference engine for the Performance Co-Pilot)
### END INIT INFO
#
# For FreeBSD
# PROVIDE: pmie
# REQUIRE: NETWORKING FILESYSTEMS pmcd
# KEYWORD: shutdown
# And add the following lines to /etc/rc.conf to run pmie:
# pmie_enable="YES"
#

. $PCP_DIR/etc/pcp.env

if [ `id -u` -eq 0 ]
then
    # Handle setup for transients that might be lost after reboot,
    # e.g. $PCP_RUN_DIR
    #
    $PCP_BINADM_DIR/pcp-reboot-init

    # do the real startup as user $PCP_USER:$PCP_GROUP
    #
    $PCP_BINADM_DIR/runaspcp "$PCP_SERVICES_DIR/pmie $*"
    exit $?
else
    if [ "$1" = status ]
    then
	$PCP_SERVICES_DIR/pmie status
    else
	echo "$0: Error: You must be root (uid 0) to start or stop pmie via this script"
	exit 1
    fi
fi

exit 0
